import router from "./router";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import pinia from "./stores";
import useUserStore from "./stores/modules/userStore";
const userStore = useUserStore(pinia);
//前置路由守卫
router.beforeEach( async (to,from,next)=>{
    nprogress.start();
    //判断是否登录

    if(userStore.userInfo.token) {
        if(to.path == "/login"){
            next("/");
            nprogress.done();
        }else {
           let userinfo = userStore.users;
          try {
            if(!userinfo) {
                await userStore.userLogin()
            nprogress.done();
            next({...to});
            }else {
            nprogress.done();
            next();
            }
          } catch (error) {
            await userStore.logOut()
            next("/login");
            nprogress.done();
          }
        }
    }else {
        if(to.path !== "/login") {
            next("/login");
            nprogress.done();
        }else {
            next();
            nprogress.done();
        }
    }
})


//后置路由守卫
router.afterEach((to,from)=>{
nprogress.done();
})